package io.kiki.sba.registry.server.clientInterface.filter.blacklist;

import io.kiki.sba.registry.common.model.store.BaseInfo;
import io.kiki.sba.registry.common.model.store.Url;
import io.kiki.sba.registry.server.clientInterface.filter.IPMatchStrategy;
import io.kiki.sba.registry.server.clientInterface.filter.ProcessFilter;
import io.kiki.sba.registry.server.clientInterface.providedata.FetchBlackListService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;


public class BlacklistMatchProcessFilter implements ProcessFilter<BaseInfo> {

    @Autowired
    protected IPMatchStrategy ipMatchStrategy;

    @Resource
    private FetchBlackListService fetchBlackListService;

    @Override
    public boolean match(BaseInfo storeData) {

        final List<BlacklistConfig> list = fetchBlackListService.getBlacklistConfigList();

        // empty list proceed
        if (null == list || list.size() == 0) {
            return false;
        }

        Url url = storeData.getClientUrl();

        if (url != null) {
            switch (storeData.getDataType()) {
            case publisher:
                return ipMatchStrategy.match(url.getIp(), () -> BlacklistConstants.FORBIDDEN_PUB);

            case subscriber:
                return ipMatchStrategy.match(url.getIp(), () -> BlacklistConstants.FORBIDDEN_SUB_BY_PREFIX);

            default:
                return false;
            }
        }
        return false;
    }
}
